package com.itfreer.arcgis.edit;

import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.data.Feature;
import com.esri.arcgisruntime.data.FeatureQueryResult;
import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.itfreer.arcgis.draw.DrawPolyLineTool;
import com.itfreer.arcgis.query.GetSelectFeatureListener;
import com.itfreer.arcgis.utils.LayerUtils;
import com.itfreer.utils.common.ToastUtils;
import com.itfreer.utils.config.ConfigManager;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * 创建分割线
 */
public class CreateSplitPolylineTool extends DrawPolyLineTool
{
    @Override
    public void Draw(final MapView mapView, final Geometry geometry)
    {
        final FeatureLayer featureLayer = EditToolUtils.getCurrentEditFeatureLayer();
        if (featureLayer == null)
        {
            ToastUtils.showShort(ConfigManager.getContext(), "当前无编辑图层！");
            return;
        }

        // 判断是否有选中元素
        LayerUtils.GetSelectFeature(featureLayer, new GetSelectFeatureListener()
        {
            @Override
            public void execute(final FeatureLayer featureLayer, ListenableFuture<FeatureQueryResult> features)
            {
                try
                {
                    final List<Feature> selectFeatures = new ArrayList<>();
                    for (Feature feature : features.get())
                        selectFeatures.add(feature);

                    if (selectFeatures.size() == 0)
                        ToastUtils.showShort(ConfigManager.getContext(), "当前编辑图层无选择要素");
                    else if(selectFeatures.size()>1)
                    {
                        ToastUtils.showShort(ConfigManager.getContext(), "请选择单个元素进行分割！");
                    }
                    else
                    {
                        CreateSplitPolylineTool.super.Draw(mapView,geometry);
                    }
                } catch (InterruptedException e)
                {
                    e.printStackTrace();
                } catch (ExecutionException e)
                {
                    e.printStackTrace();
                }
            }
        });
    }
}
